package com.giraffe.device.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.giraffe.device.api.DeviceService;
import com.giraffe.device.config.TdEngineConfig;
import com.giraffe.device.pojo.dto.DeviceDTO;
import com.giraffe.device.strategy.DeviceMessageParsingStrategy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * @Author ldx
 * @Date 2024/02/22 14:13
 **/
@RestController
@RequestMapping("test")
public class TestController {

    @Resource
    private List<DeviceMessageParsingStrategy> deviceMessageParsingStrategies;
    @Resource
    private DeviceService deviceService;
    @Resource
    private TdEngineConfig tdengineConfig;

    @PostMapping("send")
    public void send(@RequestBody JSONObject obj){
        DeviceDTO dto = deviceService.getDevice(obj.getString("deviceId"));
        DeviceMessageParsingStrategy parsingStrategy = deviceMessageParsingStrategies.stream().filter(f -> f.deviceTypeCode().equals(dto.getDeviceType())).findFirst().get();
        parsingStrategy.parsing(obj.getString("deviceId"),obj.getString("topic"),obj.getJSONObject("message").toString());
        /*try {
            Statement stmt = tdengineConfig.tdEngine();
            stmt.executeUpdate("insert into giraffe.thermometer_0x320b (ts, temperature) values(now(), 18.2)");
            tdengineConfig.tdEngineClose(stmt);
        }catch (SQLException e){

        }*/

    }
}
